﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Web;

namespace TRIG_MOD_DASHBOARDS.DocsInThisProject
{
    public partial class DocsInThisProjectUserControl : UserControl
    {
        DataForUsersTasks objCls = new DataForUsersTasks();
        public string Type { get; set; }
        private static string Entity { get; set; }
        
        bool bexport = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            Type = objCls.GetUserEntity();
            objCls.errorMsg = errorMsg;
            LoadApprovalData();
        }

        private void LoadApprovalData()
        {
            //SPFieldUserValue teskUser = new SPFieldUserValue(SPContext.Current.Site.OpenWeb(), SPContext.Current.Web.CurrentUser.LoginName);
            objCls.errorMsg = errorMsg;
            int gvCount = objCls.DocsApprovalStatus(Convert.ToString("teskUser.User"), gvAppStatus);

            if (gvCount > 0) { gvAppStatus.Visible = true; errorMsg.Text = ""; btnexport.Visible = true; }
            else 
            { gvAppStatus.DataSource = null; gvAppStatus.DataBind(); errorMsg.Text = "No records found"; errorMsg.ForeColor = System.Drawing.Color.Green;
                gvAppStatus.Visible = false; btnexport.Visible = false; }
        }
        protected override void Render(HtmlTextWriter writer)
        {
            try
            {
                if (Page != null)
                {
                    if (bexport)
                    {
                        gvAppStatus.Visible = true;
                        this.bexport = false;
                        Page.Response.Clear();
                        Page.Response.Buffer = true;
                        Page.Response.ContentType = "application/ms-excel";
                        Page.Response.AddHeader("content-disposition", "attachment; filename=Register" + DateTime.Now + ".xls");
                        Page.Response.Charset = "UTF-8";
                        DateTime ct = DateTime.Now;
                        string currentdate = Convert.ToString(ct);
                        //string companyname = getCompanyname();
                        HttpContext.Current.Response.Write("<div style='text-align:center'><b>" + "ghydg"+ "</b></div>");
                        this.EnableViewState = false;
                        //gvAppStatus.AllowPaging = false;
                        //generateregister();
                        System.IO.StringWriter sw = new System.IO.StringWriter();
                        System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
                        gvAppStatus.RenderControl(htw);
                        Page.Response.Write(sw.ToString() + "Register Date : " + currentdate + "");
                        Page.Response.End();

                    }
                }
                base.Render(writer);
            }
            catch (Exception )
            {
                //pnlerr.Controls.Add(new LiteralControl("Error in Export to Excel Render Function" + ex.Message));
                //pnlerr.Controls.Add(new LiteralControl("</Br>"));
            }
        }

        protected void gvAppStatus_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string listurl1 = string.Empty;
                string listurl2 = string.Empty;
                string listurl3 = string.Empty;
                try
                {
                    //listurl1 = "javascript:return DialogDisplay('/_layouts/TRIG_MOD_DASHBOARDS/CustomEdit.aspx?Type=Register&Entity="+ Type +"&Source=Home&DocType=" + e.Row.Cells[0].Text + "&ApprovalStatus=Approved');";
                    //listurl2 = "javascript:return DialogDisplay('/_layouts/TRIG_MOD_DASHBOARDS/CustomEdit.aspx?Type=Register&Entity="+ Type +"&Source=Home&DocType=" + e.Row.Cells[0].Text + "&ApprovalStatus=Approved with Comments');";
                    //listurl3 = "javascript:return DialogDisplay('/_layouts/TRIG_MOD_DASHBOARDS/CustomEdit.aspx?Type=Register&Entity="+ Type +"&Source=Home&DocType=" + e.Row.Cells[0].Text + "&ApprovalStatus=Not Approved');";

                    listurl1 = "javascript:return DownloadFile('/Pages/registers.aspx?Type=Register&Entity=" + Type + "&Source=Home&DocType=" + e.Row.Cells[0].Text + "&ApprovalStatus=Approved');";
                    listurl2 = "javascript:return DownloadFile('/Pages/registers.aspx?Type=Register&Entity=" + Type + "&Source=Home&DocType=" + e.Row.Cells[0].Text + "&ApprovalStatus=Approved with Comments');";
                    listurl3 = "javascript:return DownloadFile('/Pages/registers.aspx?Type=Register&Entity=" + Type + "&Source=Home&DocType=" + e.Row.Cells[0].Text + "&ApprovalStatus=Not Approved');";
                }
                catch (ArgumentException) { }
                catch (NullReferenceException) { }
                CommonMethods.CreateLinkButton(e.Row.Cells[1].Text, listurl1, e, 1, true);
                CommonMethods.CreateLinkButton(e.Row.Cells[2].Text, listurl2, e, 2, true);
                CommonMethods.CreateLinkButton(e.Row.Cells[3].Text, listurl3, e, 3, true);
            }
        }

        protected void btnexport_Click(object sender, EventArgs e)
        {
            this.bexport = true;
        }

    }
}
